********************************************************************************
*** Create cumulative discount rate by cohort 
********************************************************************************

*========================*
* Set up the environment *
*========================*

capture program drop init_usa

init_usa  // define directories


global r_const = 0.05

local nyears = $endyear - $startyear + 1


*===========================================================*
* Calculate cumulative discount rate using constant r = 0.05*
*===========================================================*

set obs `nyears'
egen year = seq(), f($startyear) t($endyear)
gen r = $r_const

CalcCumDiscnt year r, coh0($startyear) coh1($endyear)

sort cohort year

save $DataO/CumDiscount_constant, replace



*===========================================================*
* Subroutine, save as an ado file
*===========================================================*

program CalcCumDiscnt

	cap program drop CalcCumDiscnt

	syntax varlist(min=2 numeric), coh0(integer) coh1(integer)
	tokenize `varlist'
	local year `1'
	local r `2'
	

	forvalues y=`coh0'/`coh1' {
		gen CumDiscnt`y' =.
		replace CumDiscnt`y' = 1/(1+`r') if year>=`y'
		replace CumDiscnt`y' = CumDiscnt`y'[_n] * CumDiscnt`y'[_n-1] if year>`y'
	}

	reshape long CumDiscnt, i(`year') j(cohort)

	drop if CumDiscnt == .
	
	
	sort cohort year
	
end
